#include<vector>
#include<algorithm>
#include"netAddr.hpp"
#include"Log.hpp"
class Route{
    public:
        Route(){};
        ~Route(){};
        void MessageRoute(int socket_fd,const std::string &msg,netAddr &msg_sender){
            LOG(LogLevel::DEBUG)<<"in _func";
            std::string Ip=msg_sender.getIp();
            uint16_t Port=msg_sender.getPort();
            //TODO:根据Ip和Port找到对应的socket_fd并发送消息
            std::string info;
            //TODO:从socket_fd读取消息并处理
            auto it=std::find(rt.begin(),rt.end(),msg_sender);
            if(it==rt.end()){
                //没有对应的主机添加到路由表中
                rt.emplace_back(msg_sender);
                LOG(LogLevel::INFO)<<"New host connected";
                //TODO:根据Ip和Port找到对应的socket_fd并发送消息
            }
            info+=msg;
            //TODO:将消息发送给所有在线主机
            for(auto &i:rt){
                sendto(socket_fd,info.c_str(),info.size(),0,(struct sockaddr*)&i.getAddr(),sizeof(i.getAddr()));
            }
            if(msg=="QUIT"){
                //TODO:路由表中删除该主机
                rt.erase(std::remove(rt.begin(),rt.end(),msg_sender),rt.end());
                LOG(LogLevel::INFO)<<"this Ip"<<Ip<<"Port"<<Port<<" disconnected";
            }
        }
    private:
        std::vector<netAddr>rt;//在线主机
};